﻿package core.dialog {
	
	import flash.display.MovieClip;
	import base.BaseMc;
	import flash.events.MouseEvent;
	import core.net.socket.cmd.BluetoothUnBindCmd;
	import core.events.CmdEvent;
	import core.net.socket.cmd.BluetoothOfflineCmd; 
	import core.net.socket.cmd.BluetoothDataCmd;
	import core.net.socket.cmd.BluetoothDisConnectCmd;
	import core.User;
	import core.events.MessageDialogEvent;
	import core.events.FlowerUploadEvent;
	import core.net.socket.cmd.BluetoothOnlineCmd;
	import core.net.socket.cmd.BluetoothOfflineCmd;
	 
	
	public class DialogViewBluetoothSetting extends BaseMc {
		
		
		public function DialogViewBluetoothSetting() {
			// constructor code
		}
		protected override function onInit():void { 
			this.btn_upload.addEventListener(MouseEvent.CLICK,onUploadClick);
			this.btn_unbind.addEventListener(MouseEvent.CLICK,onDuankaiUnbindClick);
			this.btn_jiebang.addEventListener(MouseEvent.CLICK,onJiebangClick);
			this.centerOfParent();
			this.y=150;
			
			
		}
		public function onJiebangClick(e:MouseEvent):void {
			var dlg:DialogMessage = new DialogMessage("确定解绑吗？",false);
			dlg.addEventListener(MessageDialogEvent.OK,onJiebangByUser);
			this.stage.addChild(dlg);
		}
		
		private function onJiebangByUser(e:MessageDialogEvent):void {
			this.sendCmd(BluetoothUnBindCmd.make());
			User.deviceAddress = "";
			onDuankaiUnbindClick(null);
		}
		
		
		
		public override function show():void{
			super.show();
			this.sendCmd(BluetoothDataCmd.make(BluetoothDataCmd.BATTERY,null));
		}
		
		
		
		public function onUploadClick(e:MouseEvent):void {
			 var count= this.offflower_count.text;
			 if(count>0){
				 this.btn_upload.removeEventListener(MouseEvent.CLICK,onUploadClick);
				 this.btn_unbind.removeEventListener(MouseEvent.CLICK,onDuankaiUnbindClick);
				 this.btn_jiebang.removeEventListener(MouseEvent.CLICK,onJiebangClick);
				 this.sendCmd(BluetoothDataCmd.make(BluetoothDataCmd.FLOWER_DATA,null)); 
			 	 this.info_content.text="正在上传……";
				 this.dispatchEvent(new FlowerUploadEvent(FlowerUploadEvent.UPLOADING));
		     }
		}
		public function onDuankaiUnbindClick(e:MouseEvent):void { 
			 this.sendCmd(BluetoothDisConnectCmd.make()); 
			 DialogBluetooth.connected = false; 
			 var p:BaseMc = this.parent as BaseMc;
			 p.remove();
		}
		protected override function onCommandResponse(e:CmdEvent):void {
			super.onCommandResponse(e);
			var data:Object = e.data;
			 if(data.cmd == BluetoothDataCmd.COMMAND_ID){
				var bluetoothCmd:String = data.data.bluetoothCmd; 
				//电量 
				if(bluetoothCmd == "BATTERY"){
					  trace("电量 ："+ data.data.battery); 
					  if(data.data.battery>400){
					  	this.battery.gotoAndStop(5);
					  }else if(data.data.battery>350&&data.data.battery<=400){
					  	this.battery.gotoAndStop(4);
					  }else if(data.data.battery>300&&data.data.battery<=350){
					  	this.battery.gotoAndStop(3);
					  }else if(data.data.battery>250&&data.data.battery<=300){
					  	this.battery.gotoAndStop(2);
					  }else if(data.data.battery>200&&data.data.battery<=250){
					  	this.battery.gotoAndStop(2);
					  }else if(data.data.battery<=200){
					  	this.battery.gotoAndStop(1);
					  }
					  this.sendCmd(BluetoothDataCmd.make(BluetoothDataCmd.FLOWER_COUNT,null)); 
				}else if(bluetoothCmd == "FLOWER_COUNT"){
					//离线红花数量
					  this.offflower_count.text=data.data.count;
					  trace("离线红花数量 ："+ data.data.count);
				}else if(data.cmd == BluetoothDisConnectCmd.COMMAND_ID){
					if(data.error == ""){
					 	trace("断开成功"); 
					}else{
					 	trace("断开失败"); 
					}
				}else if(bluetoothCmd == "FLOWER_DATA"){  
					if(data.data.flower==0){
						this.info_content.text="已上传完成";
						this.offflower_count.text=0+"";
						this.dispatchEvent(new FlowerUploadEvent(FlowerUploadEvent.SUCCESS));
						this.btn_upload.addEventListener(MouseEvent.CLICK,onUploadClick);
						this.btn_unbind.addEventListener(MouseEvent.CLICK,onDuankaiUnbindClick);
						this.btn_jiebang.addEventListener(MouseEvent.CLICK,onJiebangClick);
					}else if(data.data.flower==-1){
						trace("无离线数据");
						this.offflower_count.text=0+"";
						this.dispatchEvent(new FlowerUploadEvent(FlowerUploadEvent.SUCCESS));
						this.btn_upload.addEventListener(MouseEvent.CLICK,onUploadClick);
						this.btn_unbind.addEventListener(MouseEvent.CLICK,onDuankaiUnbindClick);
						this.btn_jiebang.addEventListener(MouseEvent.CLICK,onJiebangClick);
					}else{
						if(data.data.flower_status){
							trace("继续上传");
							this.offflower_count.text=data.data.flower;
							this.sendCmd(BluetoothDataCmd.make(BluetoothDataCmd.FLOWER_DATA,null)); 
						}else{
							trace("上传失败");
							this.dispatchEvent(new FlowerUploadEvent(FlowerUploadEvent.SUCCESS));
							this.btn_upload.addEventListener(MouseEvent.CLICK,onUploadClick);
							this.btn_unbind.addEventListener(MouseEvent.CLICK,onDuankaiUnbindClick);
							this.btn_jiebang.addEventListener(MouseEvent.CLICK,onJiebangClick);
						}
					}
					
					trace("离线红花 ："+ data.data.flower);
				}
			}else if(data.cmd == BluetoothUnBindCmd.COMMAND_ID){
					if(data.error == ""){
					 
					}else{
					 
			         }
	        } 
	}
	
}
}